package com.clp.protocol.iec104.apdu.apci;

import io.netty.buffer.ByteBuf;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ICtrlArea extends CtrlArea<ICtrlArea> {
    /**
     * 发送序号
     */
    private int sendSeq;
    /**
     * 接收序号
     */
    private int recvSeq;

    @Override
    public ICtrlArea initBy(ByteBuf buf) {
        this.sendSeq = (buf.readShortLE() & 0xFFFF) >> 1;
        this.recvSeq = (buf.readShortLE() & 0xFFFF) >> 1;
        return this;
    }

    @Override
    public boolean isValid() {
        return sendSeq >= 0 && recvSeq >= 0;
    }

    @Override
    public void writeTo(ByteBuf buf) {
        buf.writeShortLE(sendSeq << 1);
        buf.writeShortLE(recvSeq << 1);
    }

    @Override
    public String toString() {
        return "发送序号：" + sendSeq + ", 接收序号：" + recvSeq;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ICtrlArea that = (ICtrlArea) o;
        return sendSeq == that.sendSeq && recvSeq == that.recvSeq;
    }

    @Override
    public int hashCode() {
        return Objects.hash(sendSeq, recvSeq);
    }
}
